package com.lz.comm.widget.camera

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationSet
import android.view.animation.ScaleAnimation
import android.widget.FrameLayout
import android.widget.ImageView
import com.lz.comm.R

/**
 * 聚焦控件
 * Created by You on 2020-02-13.
 */
class FocusView : FrameLayout {

    private lateinit var ivFocus: ImageView

    private val anim: Animation by lazy {
        val scaleAnim = ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, ivFocus.width / 2f, ivFocus.height / 2f).apply {
            duration = 300; fillAfter = true
        }
        val alphaAnim = AlphaAnimation(1.0f, 0.75f).apply { duration = 700 }
        AnimationSet(false).apply {
            addAnimation(scaleAnim)
            addAnimation(alphaAnim)
            setAnimationListener(object : AnimationListener {
                override fun onAnimationStart(animation: Animation) {
                    ivFocus.visibility = VISIBLE
                }

                override fun onAnimationEnd(animation: Animation) {
                    ivFocus.visibility = INVISIBLE
                }

                override fun onAnimationRepeat(animation: Animation) = Unit
            })
        }
    }

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context) {
        ivFocus = ImageView(context).apply {
            setImageResource(R.drawable.focus_camera)
            scaleType = ImageView.ScaleType.FIT_XY
            visibility = INVISIBLE
        }
        addView(ivFocus, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val size = when (MeasureSpec.EXACTLY) {
            MeasureSpec.getMode(widthMeasureSpec) -> MeasureSpec.getSize(widthMeasureSpec)
            MeasureSpec.getMode(heightMeasureSpec) -> MeasureSpec.getSize(heightMeasureSpec)
            else -> DEF_SIZE
        }
        val sizeSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY)
        super.onMeasure(sizeSpec, sizeSpec)
    }

    override fun onDetachedFromWindow() {
        ivFocus.clearAnimation()
        super.onDetachedFromWindow()
    }

    /**
     * 设置当前聚焦中心坐标点
     * @param fx
     * @param fy
     */
    fun setCenter(fx: Float, fy: Float) {
        val parentView = parent as View
        val left = parentView.paddingLeft
        val right = parentView.width - parentView.paddingRight - width
        val top = parentView.paddingTop
        val bottom = parentView.height - parentView.paddingBottom - height

        //不能超过边缘
        x = clamp(fx - width / 2.0f + left, left, right)
        y = clamp(fy - height / 2.0f + top, top, bottom)
        ivFocus.clearAnimation()
        ivFocus.startAnimation(anim)
    }

    /**
     * x值不能超出min~max范围
     */
    private fun clamp(v: Float, min: Int, max: Int): Float = when {
        v > max -> max.toFloat()
        v < min -> min.toFloat()
        else -> v
    }

    companion object {
        private const val DEF_SIZE = 200
    }
}